require "Common/define"
require "Logic/Config"
require "Common/functions"
---@class UnlockFuncManager
UnlockFuncManager = UnlockFuncManager or {};
local this = UnlockFuncManager;

local waitingUnlockFunc;
local funcSwitchTab;

UnlockFuncManager.unlockFuncCollect = { --待解锁的不太好走配置，策划不知道那些需要解锁时发生协议
    Config.FuncOpenType.Cost,              -- 集市
    Config.FuncOpenType.Title,             -- 勋章
    Config.FuncOpenType.SupSkill,          -- 支援技能
    Config.FuncOpenType.TimeBoxAll,        -- 时光宝箱
    Config.FuncOpenType.Friend,            -- 好友相关
    Config.FuncOpenType.Activity,          -- 活动
    Config.FuncOpenType.OperationNotice,   -- 主城公告牌
    Config.FuncOpenType.Task,              -- 任务成就
    Config.FuncOpenType.Arena,             -- 竞技场
    Config.FuncOpenType.CrossTeamArena,    -- 高阶竞技场
    Config.FuncOpenType.FriendBattle,      -- 好友战
    Config.FuncOpenType.TeamEditor,        -- 编队
    Config.FuncOpenType.BattleRecommend,   -- 阵容推荐
    Config.FuncOpenType.Restrain,          -- 共鸣
    Config.FuncOpenType.expedition,        -- 远征
    Config.FuncOpenType.RankBattle,        -- 排位战
    Config.FuncOpenType.DarkStreet,        -- 暗巷
    Config.FuncOpenType.Pet,               -- 宠物
    Config.FuncOpenType.Mail,              -- 邮件
    Config.FuncOpenType.Mythic,            -- 永劫之境
    Config.FuncOpenType.Guild,             -- 军团
    Config.FuncOpenType.Bag,               -- 给分享用的
    Config.FuncOpenType.Tower,             -- 爬塔
    Config.FuncOpenType.StoryModelReward,  -- 剧情模式
    Config.FuncOpenType.HeroManor,         -- 家园系统
}

function UnlockFuncManager.init()
    funcSwitchTab = G.dataTable["t_functionswitch"];
end

function UnlockFuncManager.initUnlockFuncList()
    if funcSwitchTab == nil then
        return;
    end
    if waitingUnlockFunc == nil then
        waitingUnlockFunc = {};
    end
    for k, v in pairs(UnlockFuncManager.unlockFuncCollect) do
        local singleConfig = funcSwitchTab[tostring(v)];
        if singleConfig ~= nil then
            if not logicMgr.FuncOpenManager.isOpen(v,false) then
                table.insert(waitingUnlockFunc, singleConfig);
            end
        end
    end
end

-- 解锁功能，需找后端要数据
function UnlockFuncManager.updateUnlockFunc()
    if waitingUnlockFunc == nil or #waitingUnlockFunc < 1 then
        return;
    end
    local hasRemoveFunc = {};
    for k, v in pairs(waitingUnlockFunc) do
        local functionId = v.f_FunctionId;
        if logicMgr.FuncOpenManager.isOpen(functionId, false) then
            if functionId == Config.FuncOpenType.Cost then
                --集市相关
                -- 用户分层
                costHandler.reqPayGrade()
                -- 集市解锁
                costHandler.reqGetMarketInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Title then
                -- 徽章解锁 -- 这条数据有点尴尬，先登录哪儿就获取
                --TitleHandler.SendTitleInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.SupSkill then
                -- 支援技能别处处理了，这儿暂时先不管了
                --skillHandler.SendReqAssistantSkill();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.TimeBoxAll then
                --时间宝箱
                TimeBoxHandler.ReqTimeBoxInfoUpdate();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Friend then
                --好友相关
                --好友数据
                friendHandler.ReqFriendData();
                --好友礼物
                friendHandler.ReqAllFriendGiftList();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Activity then
                --活动相关
                --七天登陆
                ActivityHandler.sendReqSevenDayMessage();
                --活动
                ActivityHandler.sendReqActivityInfoListMessage();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.OperationNotice then
                --主城公告牌
                NoticeHandler.ReqRedDots(true,false);
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Task then
                --任务成就
                taskInfoHandler.sendReqTaskMessage();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Arena then
                --竞技场
                arenaHandler.sendReqArenaInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.CrossTeamArena then
                --高阶竞技场
                crossTeamArenaHandler.sendReqHighArenaInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.RankBattle then
                --排位站
                RankBattleHandler.ReqRankBattlePlayerInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.FriendBattle then
                --好友战
                FriendBattleHandler.ReqTimeInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.TeamEditor then
                --编队
                logicMgr.FormationManager.req_FormationList();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.BattleRecommend then
                --阵容推荐
                partnerHandler.ReqFormationRecInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Restrain then
                --共鸣
                partnerHandler.sendFetterAwaraList();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.expedition then
                --远征
                ExpeditionHandler.ReqSynExpedition(true);
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.DarkStreet then
                --暗巷
                DarkStreetHandler.ReqDarkInfo();
                DarkStreetHandler.ReqDarkAlleyFormations();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Pet then
                --宠物
                PetHandler.ReqAllPet();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Mail then
                --邮件
                emailHandler.ReqAllMail();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Mythic then
                --永劫之境
                mythicDungeonsHandler.ReqInstanceInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Guild then
                --军团科技
                unionHandler.sendReqReqUnionTechInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Bag then
                --分享
                logicMgr.ShareManager.Req_shareTimes();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.Tower then
                --爬塔
                towerHandler.sendGetTowerInfo();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.StoryModelReward then
                --剧情模式
                dungeonHandler.ReqHardDungeonInfoHandler();
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.LongJourneyHell then
                --旋云远征深渊
                longJourneyHellHandler.reqLongJourneyHellInfo()
                --table.remove(waitingUnlockFunc, k);
                hasRemoveFunc[tostring(functionId)] = 1;
            elseif functionId == Config.FuncOpenType.HeroManor then
                -- 家园系统
                HeroManorHandler.sendReqManorInfoMessage(1);
                hasRemoveFunc[tostring(functionId)] = 1;
            end
        end
    end
    for rk, rv in pairs(hasRemoveFunc) do
        local removeIndex = -1;
        for wk, wv in pairs(waitingUnlockFunc) do
            if tonumber(wv.f_FunctionId) == tonumber(rk) then
                removeIndex = wk;
                break;
            end
        end
        if removeIndex ~= -1 then
            table.remove(waitingUnlockFunc, removeIndex);
        end
    end
    hasRemoveFunc = {};
end

function UnlockFuncManager.OnDestroy()
    if waitingUnlockFunc ~= nil then
        waitingUnlockFunc = nil;
    end
end

return UnlockFuncManager